var timeDate = "map" + new Date().getTime(); // 默认时间戳（主要应用于此场景）
var newMapDate = ""; // 生成新Echart的map（主要用于编辑和预览场景）
var mapDataRender = [];
var timer;
const delayTime = 3000;
var mapGeoJson;

// for (let i = 0; i < 3; i++) {
//   console.log(new Date().getTime())
// }



async function defineComponentBeforeCreated({ VueElement }) {
  console.log("defineComponentBeforeCreated");
  debugger
  try {
    if (VueElement) {
      // 兼容2.5的写法，已经无全局变量需要调整处理
      if (
        VueElement.component.props &&
        VueElement.component.props.mapInfo.json
      ) {
        // 刷新后读取最新的数据
        window[`${VueElement.id}mapGepJson`] =
          VueElement.component.props.mapInfo.json;
      } else {
        // 若无切换图层则使用默认的数据
        let mapUrl =
          BASE_URL +
          "/adminapp/public/assets/elfinder/files/json/geo/广州.json";
        let getMapJson = function () {
          return fetch(mapUrl).then((res) => res.json());
        };
        window[`${VueElement.id}mapGepJson`] = await getMapJson();
      }
      echarts.registerMap(VueElement.id, window[`${VueElement.id}mapGepJson`]);
    } else {
      // 兼容在组件库展示效果的写法
      let mapUrl =
        BASE_URL + "/adminapp/public/assets/elfinder/files/json/geo/广州.json";
      let getMapJson = function () {
        return fetch(mapUrl).then((res) => res.json());
      };
      mapGeoJson = await getMapJson();
      echarts.registerMap(VueElement ? VueElement.id : timeDate, mapGeoJson);
    }
  } catch (error) {
    console.log("error1", error);
  }
}

async function updateMap(data, VueInstance) {
  console.log("updateMap");
  debugger
  let mapGeoJsonData = data;
  echarts.registerMap(VueInstance.options.series[0].map, mapGeoJsonData);
  mapDataRender = mapGeoJsonData.features.map((item) => {
    return {
      value: (Math.random() * 1000).toFixed(2),
      name: item.properties.name,
    };
  });
  if (VueInstance) {
    VueInstance.options.series[0].data = mapDataRender;
    VueInstance.updateOptions(VueInstance.options);
  }
}

function defineComponentCreated({ VueInstance, VueElement }) {
  console.log("defineComponentCreated");
  debugger
  try {
    if (VueElement && VueInstance) {
      newMapDate = VueElement.id;
      if (newMapDate) {
        VueInstance.options.series[0].map = newMapDate;
        VueInstance.options.geo.map = newMapDate;
        echarts.registerMap(newMapDate, window[`${VueElement.id}mapGepJson`]);
      } else {
        echarts.registerMap(newMapDate, window[`${VueElement.id}mapGepJson`]);
      }
      VueInstance.options.series[0].data = mapDataRender;
      VueInstance.updateOptions(VueInstance.options);

      const myChart = VueInstance.getEChartInstance();
      // 依次跳转显示 - begin
      var arr = [
        "花都区",
        "从化区",
        "增城区",
        "黄埔区",
        "白云区",
        "天河区",
        "越秀区",
        "荔湾区",
        "海珠区",
        "番禺区",
        "南沙区",
      ];
      var index = 0;
      timer = setInterval(function () {
        index = index < arr.length ? index : 0;
        showTips(arr[index++]);
      }, 1000);

      myChart.on("mouseover", function () {
        clearInterval(timer);
      });

      myChart.on("mouseout", function () {
        clearInterval(timer);
        timer = setInterval(function () {
          index = index < arr.length ? index : 0;
          showTips(arr[index++]);
        }, delayTime);
      });
      // 依次跳转显示 - end

      function showTips(name) {
        mapDataRender = mapGeoJson.features.map((item) => {
          return {
            value: (Math.random() * 1000).toFixed(2),
            name: item.properties.name,
          };
        });

        mapDataRender.forEach((item, i) => {
          myChart.dispatchAction({
            type: "mapUnSelect",
            seriesIndex: 0,
            dataIndex: i,
          });
          if (item.name.includes(name)) {
            myChart.dispatchAction({
              type: "showTip",
              seriesIndex: 0,
              dataIndex: i,
            });
            myChart.dispatchAction({
              type: "mapSelect",
              seriesIndex: 0,
              dataIndex: i,
            });
          }
        });
      }
    }
  } catch (error) {
    console.log("error2", error);
  }
}

async function handleDataCallback({ ResourceData, VueInstance }) {
  console.log("handleDataCallback");
  debugger
  if (VueInstance) {
    updateMap(ResourceData, VueInstance);
  }
}
